#ifndef			__STOCKMESSAGE_H__
# define		__STOCKMESSAGE_H__
# include		"AThread.hpp"
# include		<list>

template<typename T>
class		StockMessage
{
public:
	StockMessage()
	{
		if (!Mutex::create(&_lock))
		  std::cerr << "Initialisation failed" << std::endl;
	}
	~StockMessage()
	{
		Mutex::destroy(&_lock);
	}
	
	/*
	--	Manip List with mutex (X plateform)
	*/
	void			addMessage(const T * msg)
	{
		if (!Mutex::lock(&_lock))
			std::cerr << "Can't lock this mutex" << std::endl;
		else
		{
		  this->_list.push_back((T *)msg);
			Mutex::unlock(&_lock);
		}
	}
	T *		getMessage()
	{
		T*	ret = NULL;

		if (Mutex::lock(&_lock))
		  {
			if (this->_list.size() > 0)
			  {
			    ret = this->_list.front();
			    this->_list.pop_front();
			  }
			Mutex::unlock(&_lock);
		  }
		else
		  std::cerr << "Can't lock this mutex" << std::endl;
		return (ret);
	}
	T *		getWithoutPop()
	{
		T*	tmp = NULL;

		if (Mutex::lock(&_lock))
		  {
			if (this->_list.size() > 0)
			{
			    tmp = this->_list.front();
			}
			Mutex::unlock(&_lock);
		  }
		else
		  std::cerr << "Can't lock this mutex" << std::endl;
		return (tmp);
	}
private:
	myMutex				_lock;
	std::list<T *>		_list;
};

#endif		/* __STOCKMESSAGE_H__ */
